package com.example.main

import android.content.res.Configuration
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.widget.Button
import android.widget.ImageView
import android.widget.TextView
import android.widget.Toast
import androidx.appcompat.app.AlertDialog
import androidx.navigation.fragment.NavHostFragment
import androidx.navigation.ui.setupWithNavController
import com.alibaba.android.arouter.facade.annotation.Route
import com.example.lib_base.mvvm.BaseViewActivity
import com.example.main.databinding.ActivityMainBinding
import com.example.main.fragment.MessageFragment
import com.google.android.material.bottomnavigation.BottomNavigationView
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
@Route(path = "/main/MainActivity")
class MainActivity : BaseViewActivity<ActivityMainBinding>() {
    override fun initData() {

    }
    override fun initView() {
        val navHostFragment = supportFragmentManager.findFragmentById(R.id.fragmentContainerView) as NavHostFragment
        val navController = navHostFragment.navController
        val but: BottomNavigationView = findViewById(R.id.but)
        but.setupWithNavController(navController)

        requestPermissions(arrayOf(
            "android.permission.VIBRATE",
            "android.permission.RECORD_AUDIO" ,
            "android.permission.CAMERA",
            "android.permission.ACCESS_NETWORK_STATE",
            "android.permission.WRITE_EXTERNAL_STORAGE" ,
            "android.permission.ACCESS_FINE_LOCATION",
            "android.permission.GET_TASKS" ,
            "android.permission.ACCESS_WIFI_STATE" ,
            "android.permission.CHANGE_WIFI_STATE" ,
            "android.permission.WAKE_LOCK",
            "android.permission.MODIFY_AUDIO_SETTINGS",
            "android.permission.READ_PHONE_STATE",
            "android.permission.RECEIVE_BOOT_COMPLETED",
            "android.permission.FOREGROUND_SERVICE",
            "android.permission.ACCESS_COARSE_LOCATION"
        ), 100)

        // 只在首次进入时弹窗
        val prefs = getSharedPreferences("settings", MODE_PRIVATE)
        val isUpdateDialogShown = prefs.getBoolean("isUpdateDialogShown", false)
        if (!isUpdateDialogShown) {
            showUpdateDialog()
            prefs.edit().putBoolean("isUpdateDialogShown", true).apply()
        }



    }

    private fun showUpdateDialog() {
        val dialogView: View = LayoutInflater.from(this).inflate(R.layout.dialog_update, null)
        val tvTitle = dialogView.findViewById<TextView>(R.id.tv_title)
        val tvVersionDesc = dialogView.findViewById<TextView>(R.id.tv_version_desc)
        val btnUpdate = dialogView.findViewById<Button>(R.id.btn_update)
        val btnSupport = dialogView.findViewById<Button>(R.id.btn_support)
        val btnRefuse = dialogView.findViewById<Button>(R.id.btn_refuse)
        val imgIllustration = dialogView.findViewById<ImageView>(R.id.img_illustration)
        val iv_close = dialogView.findViewById<ImageView>(R.id.iv_close)
        // 设置内容
        tvTitle.text = "版本更新"
        tvVersionDesc.text = "更新修复某些bug，更好的支持手机操作系统，解决了部分手机问题"
        val dialog = AlertDialog.Builder(this)
            .setView(dialogView)
            .setCancelable(false)
            .create()
        iv_close.setOnClickListener {
            dialog.dismiss()
        }
        btnUpdate.setOnClickListener {
            Toast.makeText(this, "开始更新...", Toast.LENGTH_SHORT).show()
            dialog.dismiss()
        }
        btnSupport.setOnClickListener {
            Toast.makeText(this, "感谢反馈！", Toast.LENGTH_SHORT).show()
            dialog.dismiss()
        }
        btnRefuse.setOnClickListener {
            dialog.dismiss()
        }
        dialog.show()

    }
}